<?php 
namespace Admin\Model;
use Think\Model;

class TypeModel extends Model
{
	// 插入商品分类信息到数据库并返回是否成功
	public function index()
	{
		$data['id'] = I('post.id');
		$data['name'] = I('post.name');
		$data['pid'] = I('post.pid');
		$data['path'] = I('post.path');
		$data['sign'] = I('post.sign');	
		
		if(trim($data['name'])!=""){
			if($data['sign']=='sign'){	//通过传来的标记来判断是新增还是修改
				$map['id'] = ['eq',$data['id']];
				$res = $this->where($map)->setField('name',$data['name']);
				if($res){
					return 'edit';	//修改成功返回
				}else{
					return 'editerror';	//修改失败返回
				}
				
			}else{
				$res = $this->add($data);
				if($res){
					if($data['pid']!=0){	
						return "success1";//添加完商品根目录后返回商品根目录添加列表
					}else{
							return "success2";	//添加完商品子分类后返回商品分类列表
						}
					
					}else{
						return "error";			//添加失败后返回
				}
			}
		}else{
			return "non";				//如果类别名为空则提示失败
		}
		
	}

	// 查询商品分类的信息并返回
	public function outlook()
	{
		return $this->select();
	}

	/**
	 * [type_del 执行删除商品分类操作]
	 * @return [type] [返回成功与否的标志]
	 */
	public function type_del()
	{
		$id = I('get.id');

		$map['path'] = ['like',"%$id%"];

		$del = $this->where($map)->select();	//判断是否存在子目录

		if(!empty($del)){
			return "non";
		}else {
			$m['id'] = ['eq',$id];
			$res = $this->where($m)->delete();
			return $res;
		}
	}
	/**
	 * [edit_type 修改商品子分类时获取子分类的数据]
	 * @return [type] [返回子分类的数组]
	 */
	public function edit_type()
	{
		$id = I('get.id');
		$map['id'] = ['eq',$id];
		$list = $this->where($map)->find();
		return $list;
	}
}